﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using QuanLyThuPhiTaiLieu.Biz;
using QuanLyThuPhiTaiLieu.Common;
using QuanLyThuPhiTaiLieu.Entity;
using Telerik.WinControls;
using Telerik.WinControls.UI;
using Telerik.WinControls.UI.Export;

namespace QuanLyThuPhiTaiLieu.Forms
{
    public partial class ViewThongKePhieuBanGiao : Form
    {
        SaveFileDialog saveFileDialog;

        public ViewThongKePhieuBanGiao()
        {
            InitializeComponent();
        }

        private void SetPreferences()
        {
            radGridView1.MasterTemplate.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
            radGridView1.ShowGroupPanel = false;
            radGridView1.MasterTemplate.EnableGrouping = false;
            radGridView1.EnableHotTracking = true;
            radGridView1.CommandCellClick += ViewDetail;
        }

        void ViewDetail(object sender, EventArgs e)
        {
            int id = Convert.ToInt32((sender as GridCommandCellElement).Value);
            Utilities.CloseForm(this, new DisplayBangKeKhaiPhiSuDung(id));
        }

        private void BuildData()
        {
            radGridView1.MasterTemplate.AllowAddNewRow = false;
            radGridView1.MasterTemplate.AutoGenerateColumns = false;

            GridViewSummaryItem summaryThanhTien = new GridViewSummaryItem("ThanhTien", "Tổng = {0: #,#}", GridAggregateFunction.Sum);

            GridViewSummaryRowItem summaryRowItem = new GridViewSummaryRowItem(new GridViewSummaryItem[] { summaryThanhTien });
            this.radGridView1.SummaryRowsTop.Add(summaryRowItem);

            var biz = new PhieuBanGiaoTaiLieuBiz();
            var tem = biz.ViewAll();
            radGridView1.MasterTemplate.DataSource = tem;
        }

        private void FormLoad(object sender, EventArgs e)
        {
            var parent = MdiParent as MDI;
            if (parent != null)
            {
                //Utilities.ResetBackColor(parent.TopMenu, Constants.MenuIndex.TaiLieuVanBanPhapLy);
                parent.Status.Text = Constants.FormStatus.ThongKePhieuBanGiao;
            }

            SetPreferences();
            BuildData();

            CheckSecurity();
        }

        private void CheckSecurity()
        {
            var parrent = MdiParent as MDI;

            if (parrent == null) return;

            //if (!string.Equals(Constants.Securities.QuanLyVanBanPhapLyVaKeHoach, parrent.CurrentUser.QuyenTruyCap, StringComparison.InvariantCultureIgnoreCase)
            //    && !string.Equals(Constants.Securities.Administrator, parrent.CurrentUser.QuyenTruyCap, StringComparison.InvariantCultureIgnoreCase))
            //{
            //    buttonEdit.Visible = false;
            //    buttonRemove.Visible = false;
            //    radButton2.Visible = false;
            //}
        }

        private void Print(object sender, EventArgs e)
        {
            saveFileDialog = new SaveFileDialog { Filter = "Excel (*.xlsx)|*.xlsx" };

            if (saveFileDialog.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            if (saveFileDialog.FileName.Equals(String.Empty))
            {
                RadMessageBox.SetThemeName(this.radGridView1.ThemeName);
                RadMessageBox.Show("Hãy nhập tên file báo cáo!");
                return;
            }

            string fileName = this.saveFileDialog.FileName;

            var file = new FileInfo(fileName);

            if (file.Extension.Equals(".xlsx"))
            {
                if (File.Exists(fileName))
                {
                    File.Delete(fileName);
                }

                RunExportToExcelML(fileName);
            }
            else
            {
                RadMessageBox.SetThemeName(this.radGridView1.ThemeName);
                RadMessageBox.Show("Sai kiểu báo cáo!");
            }
        }

        private void RunExportToExcelML(string fileName)
        {
            var excelExporter = new ExportToExcelML(this.radGridView1);

            //set exporting visual settings
            excelExporter.ExportVisualSettings = true;

            try
            {
                foreach (var row in radGridView1.Rows)
                {
                    row.IsVisible = false;
                }

                foreach (var childRow in radGridView1.ChildRows)
                {
                    childRow.IsVisible = true;
                }

                //radGridView1.Rows

                excelExporter.SummariesExportOption = SummariesOption.ExportAll;
                excelExporter.HiddenRowOption = HiddenOption.DoNotExport;
                excelExporter.HiddenColumnOption = HiddenOption.DoNotExport;
                excelExporter.RunExport(fileName);

                foreach (var row in radGridView1.Rows)
                {
                    row.IsVisible = true;
                }

                RadMessageBox.SetThemeName(radGridView1.ThemeName);
                RadMessageBox.Show("Tạo báo cáo thành công.", "Báo cáo");
            }
            catch (IOException ex)
            {
                RadMessageBox.SetThemeName(this.radGridView1.ThemeName);
                RadMessageBox.Show(this, ex.Message, "I/O Error", MessageBoxButtons.OK, RadMessageIcon.Error);
            }
        }
    }
}
